<template>
  <div class="service-register-container">
    <h2>服务人员注册</h2>
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="130px"
      @submit.prevent="submitForm"
    >
      <!-- 用户名 -->
      <el-form-item label="用户名" prop="username">
        <el-input v-model="formData.username" placeholder="请输入用户名" />
      </el-form-item>

      <!-- 密码 -->
      <el-form-item label="密码（6位数字）" prop="password">
        <el-input
          v-model="formData.password"
          type="password"
          show-password
          placeholder="请输入6位数字密码"
        />
      </el-form-item>

      <!-- 确认密码 -->
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input
          v-model="formData.confirmPassword"
          type="password"
          show-password
          placeholder="请再次输入密码"
        />
      </el-form-item>

      <!-- 手机号 -->
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="formData.phone" placeholder="请输入手机号" />
      </el-form-item>

      <!-- 姓名 -->
      <el-form-item label="真实姓名" prop="realName">
        <el-input v-model="formData.realName" placeholder="请输入真实姓名" />
      </el-form-item>

      <!-- 身份证号码 -->
      <el-form-item label="身份证号" prop="idCard">
        <el-input v-model="formData.idCard" placeholder="请输入身份证号码" />
      </el-form-item>

      <!-- 性别 -->
      <el-form-item label="性别" prop="gender">
        <el-radio-group v-model="formData.gender">
          <el-radio :label="1">男</el-radio>
          <el-radio :label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>

      <!-- 出生日期 -->
      <el-form-item label="出生日期" prop="birthDate">
        <el-date-picker
          v-model="formData.birthDate"
          type="date"
          placeholder="请选择出生日期"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <!-- 所属机构 -->
      <el-form-item label="所属机构" prop="organization">
        <el-input v-model="formData.organization" placeholder="请输入所属机构名称" />
      </el-form-item>

      <!-- 工种 -->
      <el-form-item label="工种" prop="jobType">
        <el-select v-model="formData.jobType" placeholder="请选择工种">
          <el-option label="护理员" value="nurse" />
          <el-option label="康复师" value="rehab" />
          <el-option label="社工" value="social_worker" />
          <el-option label="医生" value="doctor" />
          <el-option label="志愿者" value="volunteer" />
        </el-select>
      </el-form-item>

      <!-- 提交按钮 -->
      <el-form-item>
        <el-button type="primary" native-type="submit" style="width: 100%">提交注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();
const formRef = ref(null);

const formData = ref({
  username: '',
  password: '',
  confirmPassword: '',
  phone: '',
  realName: '',
  idCard: '',
  gender: null,
  birthDate: '',
  organization: '',
  jobType: ''
});

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { pattern: /^\d{6}$/, message: '密码必须是6位数字', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value, callback) => {
        if (value !== formData.value.password) {
          callback(new Error('两次输入的密码不一致'));
        } else {
          callback();
        }
      },
      trigger: 'blur'
    }
  ],
  phone: [
    { required: true, message: '手机号不能为空', trigger: 'blur' }
  ],
  realName: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' }
  ],
  idCard: [
    { required: true, message: '请输入身份证号码', trigger: 'blur' },
    {
      pattern: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
      message: '身份证号码格式不正确',
      trigger: 'blur'
    }
  ],
  gender: [
    { required: true, message: '请选择性别', trigger: 'change' }
  ],
  birthDate: [
    { required: true, message: '请选择出生日期', trigger: 'change' }
  ],
  organization: [
    { required: true, message: '请输入所属机构名称', trigger: 'blur' }
  ],
  jobType: [
    { required: true, message: '请选择工种', trigger: 'change' }
  ]
};

// 提交表单逻辑
const submitForm = async () => {
  try {
    await formRef.value.validate();

    const { confirmPassword, ...submitData } = formData.value;

    // 模拟发送请求
    const response = await fetch('/api/v1/users/register', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(submitData)
    });

    if (!response.ok) {
      throw new Error('注册失败');
    }

    alert('注册成功！');
    router.push('/login'); // 注册成功后跳转登录页或其他页面
  } catch (error) {
    console.error(error);
    alert('注册过程中发生错误，请重试。');
  }
};
</script>

<style scoped>
.service-register-container {
  max-width: 600px;
  margin: 40px auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}
</style>
